/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controllers;

import Controllers.utils.URLUtils;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Anthares
 */
public abstract class BaseServlet extends HttpServlet {
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     * <p/>
     * @param request  servlet request
     * @param response servlet response
     * <p/>
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException      if an I/O error occurs
     */
    protected abstract void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

    protected String getLocaleString(HttpServletRequest request) {
        HttpSession httpSess;
        return (((httpSess = request.getSession(false)) == null) ? (String) getServletContext().getAttribute("default_lang") : (String) httpSess.getAttribute("lang"));
    }

    protected void handleError(int errorCode, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("errorCode", errorCode);
        request.getRequestDispatcher("/error.jsp").forward(request, response);
    }

    protected void dispatch(String ressource, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher(ressource).forward(request, response);
    }

    protected void answerAsJSON(String answer, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setAttribute("jsonText", answer);
        request.getRequestDispatcher("/json.jsp").forward(request, response);
    }

    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request  servlet request
     * @param response servlet response
     * <p/>
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException      if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("locale", getLocaleString(request));
        processRequest(request, response);
        request.setAttribute("requestedURI", URLUtils.getRequestedURI(request, getServletContext()));
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request  servlet request
     * @param response servlet response
     * <p/>
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException      if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setAttribute("locale", getLocaleString(request));
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
